package stock;

import java.util.ArrayList;
import java.util.List;

public class SequenceCounter {
	public static List<SequenceResult> findSequences(int[] sequence, int threshold) {
		List<SequenceResult> results = new ArrayList<>();
		int startIndex = -1; // 当前连续段的起始索引
		int count = 0; // 计数器
		for (int i = 0; i < sequence.length; i++) {
			if (sequence[i] > threshold) {
				if (startIndex == -1) {
					startIndex = i; // 设置起始索引
				}
				count++; // 增加计数器
			} else {
				if (startIndex != -1) {
					results.add(new SequenceResult(startIndex, count)); // 将结果添加到列表中
					startIndex = -1; // 重置起始索引
					count = 0; // 重置计数器
				}
			}
		}
		if (startIndex != -1) {
			results.add(new SequenceResult(startIndex, count)); // 处理最后一个连续段
		}
		return results;
	}

	public static void main(String[] args) {
		int[] sequence = { 1, 2, 3, 4, 5, 6, 2, 7, 8, 9, 10 };
		int threshold = 2;
		List<SequenceResult> resultList = findSequences(sequence, threshold);
		System.out.println("连续大于" + threshold + "的段数： " + resultList.size());
		for (SequenceResult result : resultList) {
			System.out.println("起始索引：" + result.getStartIndex() + ", 长度：" + result.getLength());
		}
	}
}

class SequenceResult {
	private int startIndex; // 连续段的起始索引
	private int length; // 连续段的长度

	public SequenceResult(int startIndex, int length) {
		this.startIndex = startIndex;
		this.length = length;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public int getLength() {
		return length;
	}
}
